// pages/person/moneyService/index.js
const $api = require('../../../../request/api.js').API;
import { formatTime } from '../../../../utils/util';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    cashCount: '',
    balance: '',
    unBalance: '',
    cashingCount: '',
    amount: '',
    cashOutAmount: '',
    cashMouthCount: '',
    beginMonth: '',
    page: {
      pageNo: 1,
      pageSize: 7,
      totalPage: 1
    },
    list: [],
    currentDate: new Date().getTime(),
    datePopup: false,
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // this.getData()
  },
  async init() {
    await this.getUserAccount();
    await this.getOrderSplitList();
  },
  async getUserAccount() {
    const res = await $api.getUserAccount();
    const { balance, unBalance, cashOutAmount, cashCount, cashingCount, cashMouthCount } = res.data.data;
    this.setData({
      cashingCount,
      balance,
      unBalance,
      cashOutAmount,
      amount: parseFloat((balance + unBalance).toFixed(2)),
      cashMouthCount,
      cashCount
    });
  },
  async getOrderSplitList() {
    const { beginMonth, page, list } = this.data;
    const { pageNo, pageSize } = page;
    const params = {
      pageNo,
      pageSize,
      beginMonth
    };
    const res = await $api.orderSplitList(params);
    const data = res.data.data;
    page.totalPage = data.totalPage;
    this.setData({
      list: pageNo == 1 ? data.list : list.concat(data.list),
      page
    });
  },
  toWithdrawal() {
    wx.navigateTo({
      url: '../withdrawal/index'
    });
  },
  toMoneyList() {
    wx.navigateTo({
      url: '../moneyList/index'
    });
  },
  toMoneyDetail(e) {
    wx.navigateTo({
      url: '../moneyDetail/index?id=' + e.currentTarget.dataset.id
    });
  },
  showDatePopup() {
    this.setData({
      datePopup: true
    });
  },
  cancelDatePopup() {
    this.setData({
      datePopup: false
    });
  },
  async confirmDate(e) {
    const { detail } = e;
    this.setData({
      beginMonth: detail ? formatTime(e.detail, 'year-month') : '',
      pageNo: 1
    });
    this.cancelDatePopup();
    await this.getOrderSplitList();
    wx.pageScrollTo({
      scrollTop: 0
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const { page } = this.data;
    const { pageNo, totalPage } = page;
    if (pageNo < totalPage) {
      page.pageNo = pageNo + 1;
      this.setData({
        page
      });
      this.getOrderSplitList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
});
